<?php
/**
 * Created by PhpStorm.
 * User: tom_xia
 * Date: 2018/6/4
 * Time: 13:21
 * pv统计入库
 */

require_once dirname(__DIR__)."/common.php";
use app\lib\db\DbException;
use app\cls\tom\Queue;
use app\cls\tom\SysResource;
use app\cls\tom\Process;

$process = new Process(__FILE__);
$process->setProcessNum(2);
$process->initProcess();

$redis = SysResource::getRedis();
$db =  SysResource::getDB(true);
$queue = new Queue($redis,REDIS_PREFIX."PV");
$queueNum = $queue->getQueueNum();

try{
    while(true){
        for($i = 0 ;$i< $queueNum;$i++){
            for($j = 0 ;$j < 100;$j++){
                $dataStr = $queue->pop($i);
                if(empty($dataStr)){
                    break;
                }
                echo $dataStr."\n";
                $result =  json_decode($dataStr,TRUE);
                if($result&&in_array($result['xm'],['host'])){
                    $data['log_uid'] = isset($result['uid']) ? $result['uid'] : '';
                    $ip = isset($result['ip'])&&$result['ip'] ? explode(",",$result['ip']) : null;
                    $data['log_ip'] = $ip&&isset($ip[0])&&$ip[0] ? $ip[0] : '0.0.0.0';
                    $data['log_project'] = isset($result['xm'])&&$result['xm'] ? $result['xm'] : '0';
                    $data['log_position'] = isset($result['wz']) ? $result['wz'] : '';
                    $data['log_platform'] = isset($result['pt']) ? $result['pt'] : '';
                    $data['log_operate'] = isset($result['op']) ? $result['op'] : '';
                    $data['log_uniqueid'] = isset($result['uniqueid']) ? $result['uniqueid'] : '';
                    $data['log_other'] = isset($result['other']) ? $result['other'] : '';
                    $data['log_time'] = isset($result['time'])&&$result['time'] ? $result['time'] : date("Y-m-d H:i:s");
                    $data['log_addtime'] =date("Y-m-d H:i:s");
                    $db->insert("data_logs_2018",$data);
                }
            }
        }
        sleep(5);
        $db->reconnect();
//        $process->checkProcessTime(true);
    }
}catch (DBException $e){
    if(!empty($dataStr)){
        $queue->push($dataStr,1,"right");
    }
    $process->log($e->getMessage());
    $process->restartProcess();
}catch (Exception $e){
    $process->log($e->getMessage());
}
